//
//  MinePersonInformationCell.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/28.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MinePersonInformationCell.h"
#import "RuleViewController.h"
@interface MinePersonInformationCell ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIImageView *badgeImage;//徽章
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *genderLabel;
@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *balanceLabel;//派乐宝余额
@property (nonatomic, strong) UIButton *reminderLabel;//已开通  未开通



@end

@implementation MinePersonInformationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _bgView = [UIImageView new];
        _bgView.image = IMAGE(@"mine_top_img");
        _bgView.userInteractionEnabled = YES;
        [self addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.equalTo(@193);
        }];
        
        //徽章
        _badgeImage = [UIImageView new];
        _badgeImage.image = IMAGE(@"mine_badge_default");
        [_bgView addSubview:_badgeImage];
        [_badgeImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView);
            make.left.equalTo(_bgView).offset(38);
            make.width.equalTo(@28);
            make.height.equalTo(@45);
        }];
        
        _ruleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ruleBtn setTitle:@"·了解规则·" forState:0];
        [_ruleBtn setTitleColor:ColorFrom16(0xCB9700) forState:0];
        _ruleBtn.titleLabel.font = PFRFONT(9);
        [_bgView addSubview:_ruleBtn];
        [_ruleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(10);
            make.right.equalTo(_bgView).offset(-20);
            make.width.equalTo(@47.7);
            make.height.equalTo(@13);
        }];
        
        _headerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _headerBtn.layer.backgroundColor = ColorClear.CGColor;
        _headerBtn.layer.cornerRadius = 47 / 2;
        _headerBtn.layer.masksToBounds = YES;
        [_headerBtn setImage:IMAGE(@"mine_header_default") forState:0];
        [_bgView addSubview:_headerBtn];
        [_headerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(60);
            make.left.equalTo(_bgView).offset(50);
            make.width.equalTo(@47);
            make.height.equalTo(@47);
        }];
        
        _nameLabel = [UILabel new];
        _nameLabel.text = @"Andy";
        _nameLabel.text = USER_NAME;
        _nameLabel.textColor = ColorFrom16(0x202020);
        _nameLabel.font = PFRFONT(16);
        [_bgView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(70);
            make.left.equalTo(_headerBtn.mas_right).offset(7);
            make.width.lessThanOrEqualTo(@80);
            make.height.equalTo(@22);
        }];
        
        _genderLabel = [UILabel new];
        if([BABYSEX isEqualToString:@"1"])
            _genderLabel.text = @"男";
        else
            _genderLabel.text = @"女";
        _genderLabel.textColor = ColorFrom16(0xCB9700);
        _genderLabel.font = PFRFONT(12);
        [_bgView addSubview:_genderLabel];
        [_genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom).offset(2);
            make.left.equalTo(_headerBtn.mas_right).offset(7);
            make.width.equalTo(@13);
            make.height.equalTo(@17);
        }];
        
        UIView *line = [UIView new];
        line.backgroundColor = ColorFrom16(0xCB9700);
        [_bgView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom).offset(3);
            make.left.equalTo(_genderLabel.mas_right).offset(2);
            make.width.equalTo(@0.5);
            make.height.equalTo(@15);
        }];
        
        _ageLabel = [UILabel new];
        _ageLabel.text = @"26岁";
        _ageLabel.textColor = ColorFrom16(0xCB9700);
        _ageLabel.font = PFRFONT(12);
        [_bgView addSubview:_ageLabel];
        [_ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom).offset(2);
            make.left.equalTo(line.mas_right).offset(2);
            make.width.equalTo(@28);
            make.height.equalTo(@17);
        }];
        
        
        _moneyLabel = [UILabel new];
        _moneyLabel.text = @"2000";
        _moneyLabel.textColor = ColorFrom16(0x987100);
        _moneyLabel.font = [UIFont fontWithName:@"Avenir-Heavy" size:24];
        [_bgView addSubview:_moneyLabel];
        [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(42 + 28);
            make.left.equalTo(_ageLabel.mas_right).offset(47.5);
            make.width.greaterThanOrEqualTo(@22);
            make.height.equalTo(@20);
        }];
        
        _balanceLabel = [UILabel new];
        _balanceLabel.text = @"派乐宝余额/￥";
        _balanceLabel.textColor = ColorFrom16(0xAA7E00);
        _balanceLabel.font = PFRFONT(16);
        [_bgView addSubview:_balanceLabel];
        [_balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_moneyLabel.mas_bottom).offset(1);
            make.left.equalTo(_ageLabel.mas_right).offset(47.5);
            make.width.equalTo(@104.5);
            make.height.equalTo(@22);
        }];
        
        _reminderLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        [_reminderLabel setTitle:@"已开通" forState:0];
        [_reminderLabel setTitleColor:ColorFrom16(0x9F7600) forState:0];
        _reminderLabel.titleLabel.font = PFRFONT(12);
        [_bgView addSubview:_reminderLabel];
        [_reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_balanceLabel.mas_bottom).offset(25);
            make.right.equalTo(_bgView).offset(-63);
            make.width.greaterThanOrEqualTo(@38.5);
            make.height.equalTo(@17);
        }];
        
        //优惠券
        _discountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_discountBtn setImage:IMAGE(@"mine_ discount") forState:0];
        _discountBtn.layer.backgroundColor = ColorFrom16(0xFFF8DE).CGColor;
        _discountBtn.layer.cornerRadius = 6;
        [_discountBtn setTitleColor:ColorFrom16(0x666666) forState:0];
        _discountBtn.titleLabel.font = PFRFONT(14);
        [self addSubview:_discountBtn];
        [_discountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView.mas_bottom).offset(13);
            make.left.equalTo(self).offset(10);
            make.width.equalTo(@((GWIDTH - 34) / 3));
            make.height.equalTo(@80);
        }];
        
        //充值派乐宝
        _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rechargeBtn setImage:IMAGE(@"mine_ recharge") forState:0];
        _rechargeBtn.layer.backgroundColor = ColorFrom16(0xE7FBFF).CGColor;
        _rechargeBtn.layer.cornerRadius = 6;
        [_rechargeBtn setTitleColor:ColorFrom16(0x666666) forState:0];
        _rechargeBtn.titleLabel.font = PFRFONT(14);
        [self addSubview:_rechargeBtn];
        [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_discountBtn);
            make.left.equalTo(_discountBtn.mas_right).offset(7);
            make.width.equalTo(@((GWIDTH - 34) / 3));
            make.height.equalTo(@80);
        }];
        
        //收藏
        _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectBtn setImage:IMAGE(@"mine_collection") forState:0];
        _collectBtn.layer.backgroundColor = ColorFrom16(0xFFEFF2).CGColor;
        _collectBtn.layer.cornerRadius = 6;
        [_collectBtn setTitleColor:ColorFrom16(0x666666) forState:0];
        _collectBtn.titleLabel.font = PFRFONT(14);
        [self addSubview:_collectBtn];
        [_collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_discountBtn);
            make.left.equalTo(_rechargeBtn.mas_right).offset(7);
            make.width.equalTo(@((GWIDTH - 34) / 3));
            make.height.equalTo(@80);
        }];
        
        [self initButton:_rechargeBtn];
        [self initButton:_discountBtn];
        [self initButton:_collectBtn];
    }
    return self;
}

-(void)initButton:(UIButton*)btn{
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height  ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
}

- (void)configView
{
    [_headerBtn sd_setImageWithURL:[NSURL URLWithString:STRING(USER_HEAD)] forState:0 placeholderImage:IMAGE(@"header_default")];
    if([BABYSEX isEqualToString:@"1"])
        _genderLabel.text = @"男";
    else
        _genderLabel.text = @"女";
    _ageLabel.text = [NSString stringWithFormat:@"%@岁", USER_AGE];
    _nameLabel.text = STRING(USER_NAME);
    _moneyLabel.text = USER_BALANCE;
    [_reminderLabel setTitle:USER_VIP forState:0];
    
    if([USER_VIP isEqualToString:@"已开通"])
    {
        _badgeImage.image = IMAGE(@"mine_badge");
    }
    else
    {
        [_reminderLabel setTitle:@"立即开通" forState:0];
        _reminderLabel.backgroundColor = ColorFrom16(0xFFF6C4);
        [_reminderLabel setTitleColor:ColorFrom16(0x9F7600) forState:0];
        _reminderLabel.layer.cornerRadius = 17;
        _reminderLabel.layer.masksToBounds = YES;
        [_reminderLabel addTarget:self action:@selector(handleReminder) forControlEvents:UIControlEventTouchUpInside];
        [_reminderLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@80);
            make.height.equalTo(@34);
            make.top.equalTo(_balanceLabel.mas_bottom).offset(22);
        }];
        
        _badgeImage.image = IMAGE(@"mine_badge_default");
    }
}


- (void)handleReminder
{
    RuleViewController *rule = [RuleViewController new];
    rule.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:rule animated:YES];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
